home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / mthr25 / mthread.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-08  |  4.7 KB  |  156 lines

  1. /* MicroThread V2.4
  2.  
  3.   A minimal Turbo C multithreading library for DOS.
  4.   Copyright 1993,1994,1995 I H Ting. Please feel free to use
  5.   this code in anyway that you see fit. Just don't hold me
  6.   responsible for ANYTHING. The only guarantee that I will
  7.   give is that the code will crash from time to time. You have
  8.   been warned!
  9.  
  10.   Comments and suggestions to :
  11.       Internet Email : i.h.ting@wlv.ac.uk
  12.       Compuserve     : 100023, 3363
  13.  
  14.       Otherwise      : I H Ting
  15.                        University of Wolverhampton
  16.                        School of Computing & I. T.
  17.                        Wulfruna Street
  18.                        Wolverhampton WV1 1SB
  19.                        U.K.
  20. */
  21.  
  22. #ifndef MTHREAD_H
  23. #define MTHREAD_H
  24. #define MTHREAD 24
  25.  
  26. #ifndef __STDIO_H
  27. #include <stdio.h>
  28. #endif
  29.  
  30.  
  31. /* pointer type to a thread function */
  32. typedef int (far *PThreadFunc) (void *pArg);
  33.  
  34. /* The semaphore type. */
  35. typedef unsigned int Semaphore;
  36.  
  37. /* MicroThread termination codes.
  38.    Returned by MTStartMultiThreading() */
  39. enum MTReturnCode { NORMAL, DEADLOCKED, CTRLBREAK,
  40.                     NOT_INITIALISED,TOO_DEEP_CRITICAL_NEST };
  41.  
  42. /* These are the functions you can call */
  43.  
  44. /* This initialised the MicroThread system,
  45.    but doesn't start multithreading */
  46. void MTInitialise(void);
  47.  
  48. /* Adds a new thread to the READY queue */
  49. int  MTAddNewThread(PThreadFunc pThreadFunc,unsigned priority,
  50.                     unsigned sizeArg, void *pArg);
  51.  
  52. /* This is just a convenience to avoid casting */
  53. #define MTAddThread(f,p) MTAddNewThread((PThreadFunc)f,p,0,NULL)
  54. #define MTAddArgThread(f,p,s,a) MTAddNewThread((PThreadFunc)f,p,s,a)
  55.  
  56. /* Starts multi-threading */
  57. enum MTReturnCode MTStartMultiThreading(void);
  58.  
  59. /* Kill a thread */
  60. void MTKillThread(unsigned threadID);
  61.  
  62. /* Kills the current thread */
  63. void MTEndThread(void);
  64.  
  65. /* Kills all known threads dead*/
  66. void MTEndMultiThreading(void);
  67.  
  68. /* Creates a semaphore dor application use. Returns 0 on error */
  69. Semaphore MTCreateSema(void);
  70.  
  71. /* Destroys a semaphore.
  72.    Destroying a semaphore still in use is an error!
  73.    returns 1 on success and 0 on failure. */
  74. unsigned MTDestroySema(Semaphore semaphore);
  75.  
  76. /* waits for a semaphore */
  77. void MTWait(Semaphore Semaphore);
  78.  
  79. /* waits for a semaphore */
  80. int MTTestAndSet(Semaphore Semaphore);
  81.  
  82. /* signals a semaphore, wakes up a thread waiting for
  83.    the semaphore */
  84. void MTSignal(Semaphore Semaphore);
  85.  
  86. /* enters critical section.
  87.    Go to single-threading mode temporarily for calling function
  88.    Zero return value indicates failure - too many nested
  89.    critical sections - > MAX_CRITICAL_NESTING  */
  90. unsigned MTEnterCritical(void);
  91.  
  92. /* leaves critcal section.
  93.    Reverts back to mutli-threading. */
  94. void MTLeaveCritical(void);
  95.  
  96. /* Elective yield */
  97. void MTYield(void);
  98.  
  99. /* Enables/disables preemption.
  100.    Must not be called whilst mutli-threading */
  101. void MTSetPreemptive(int bPreempt);
  102.  
  103. /* Return the semaphoire used to serialise access to
  104.    Turbo C's runtime libraries */
  105. Semaphore MTGetCRTLibSema(void);
  106.  
  107. /* returns the number of clock ticks since MTStartMultiThreading.
  108.    One second is approx. 18.2 clock ticks */
  109. unsigned long MTGetClockTick(void);
  110.  
  111. /* Put the current thread to sleep for a length of time */
  112. void MTSleep(unsigned long ticks);
  113.  
  114. unsigned MTGetMailboxHandle(char *mbxName);
  115. int MTSendMsg(unsigned mailboxHnd, unsigned msgSize, void *pMsg);
  116. int MTReceiveMsg(unsigned mailboxHnd, unsigned bufferSize, void *pBuffer);
  117. unsigned MTCreateMailbox(char *name);
  118. unsigned MTDestroyMailbox(unsigned mailboxHnd);
  119.  
  120.  
  121. /* These are C runtime library replacements.
  122.    They are basically semaphored-wrappers around
  123.    the C runtime library functions */
  124. int MTfprintf (FILE *stream, const char *format,...);
  125. int MTprintf (const char *format,...);
  126. int MTfputs(const char *s, FILE *stream);
  127. int MTfputc(int c, FILE *stream);
  128. FILE *MTfopen(const char *filename, const char *mode);
  129. int MTfclose(FILE *stream);
  130. int MTfeof(FILE *stream);
  131. int MTsprintf (char *buffer, const char *format,...);
  132. char *MTfgets(char *s, int n, FILE *stream);
  133. int MTfgetc(FILE *stream);
  134. int MTxyputc(int x, int y, int c);
  135. int MTxyputs(int x, int y, const char *s);
  136. int MTxyprintf (int x, int y, const char *format, ...);
  137. int MTkbhit(void);
  138. int MTgetch(void);
  139. char * MTgets(char *buf);
  140. void MTclrscr(void);
  141. int MTrandom(int num);
  142. void *MTmalloc(size_t size);
  143. void MTfree(void *block);
  144. char *MTstrcpy(char *source, char *dest);
  145. size_t MTfread(void *ptr, size_t size, size_t n, FILE *stream);
  146. size_t MTfwrite(const void *ptr, size_t size, size_t n, FILE*stream);
  147.  
  148. #ifndef MTCRTLIB_C
  149. #define MTputchar(c) MTfputc(c,stdout)
  150. #define MTputc(c)    MTfputc(c,stdout)
  151. #define MTputs(s)    MTfputs(s,stdout)
  152. #endif
  153.  
  154. #endif /* MTHREAD_H */
  155.  
  156.